package setCoveringProblem.gridgain;

import org.gridgain.grid.Grid;
import org.gridgain.grid.GridException;
import org.gridgain.grid.GridFactory;
import org.gridgain.grid.GridTaskFuture;
import org.gridgain.grid.gridify.GridifyArgumentAdapter;

public class GridifyCoverTaskRun {  

	public static void main(String[] args) throws GridException {
		int no_skills = 20;
		if (args.length == 0) {
			no_skills = 40;
		} else if (args.length == 1) {
			no_skills = Integer.parseInt(args[0]) * 2;
		} else {
			System.out.println("Usage: cover <size>");
			System.exit(1);
		}

		int no_elems = no_skills / 2;
		OrcaRandom rand = new OrcaRandom();

		byte[][] skills = new byte[no_elems][no_skills];

		for (int i = 0; i < no_elems; i++) {
			for (int j = 0; j < no_skills; j++) {
				byte c = (byte) ((rand.nextInt() % 1000) > 600 ? 1 : 0);
				skills[i][j] = c;
			}
		}

		GridFactory.start();
		try {
			Grid grid = GridFactory.getGrid();
			GridifyArgumentAdapter gaa = new GridifyArgumentAdapter();
			gaa.setMethodParameterTypes(byte[][].class, Integer.class);
			gaa.setMethodParameters(skills, no_elems);	

			GridTaskFuture<DatosSetCoveringProblem> future = grid.execute(GridifyCoverTask.class, gaa);

			DatosSetCoveringProblem dscp = future.get();
			if (dscp.hasCantidadOptima()) {
				System.out.println(">>> Cantidad de empleados óptima: " + dscp.getCantidadOptima());
			} else {
				System.out.println(">>> No hay una solucion optima! :(");
			}

		} finally {
			GridFactory.stop(true);
		}
	}
}  
